﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

/// <summary>
/// Summary description for Users
/// </summary>

namespace VBDWebMobile
{

    public class Users
    {

        private SqlConnection conn;
        private SqlCommand cmd;
        private string szConnection = "";

        public Users()
        {
            szConnection = ConfigurationManager.AppSettings["ConnectionString"];
            conn = new SqlConnection(szConnection);
        }

        public bool CheckUser(string sName, string sPassWord)
        {
            try
            {
                conn.Open();
                SqlDataReader myReader;
                string cmdString =
                    "SELECT [MaTK], [Ten] ,[MatKhau] FROM [FleetManagementSystem].[dbo].[TaiKhoan] WHERE" +
                    " (([Ten] = @Ten) AND ([MatKhau] = @MatKhau))";
                cmd = new SqlCommand(cmdString, conn);
                cmd.Parameters.Add("@Ten", SqlDbType.VarChar, 50);
                cmd.Parameters["@Ten"].Value = sName;
                cmd.Parameters.Add("@MatKhau", SqlDbType.VarChar, 50);
                cmd.Parameters["@MatKhau"].Value = sPassWord;
                myReader = cmd.ExecuteReader();
                if (myReader.Read())
                {
                    HttpContext.Current.Session["UserID"] = myReader.GetInt32(0);
                    myReader.Close();
                    return true;
                }
                HttpContext.Current.Session["UserID"] = null;
                return false;
            }
            catch (Exception)
            {

                conn.Close();
            }

            conn.Close();

            return true;
        }
    }
}